<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>存储类指定符</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"c/language/storage_duration","wgTitle":"c/language/storage duration","wgCurRevisionId":59319,"wgArticleId":1076,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"c/language/storage_duration","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-c {line-height: normal;}
.source-c li, .source-c pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for c
 * CSS class: source-c, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.c.source-c .de1, .c.source-c .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.c.source-c  {font-family:monospace;}
.c.source-c .imp {font-weight: bold; color: red;}
.c.source-c li, .c.source-c .li1 {font-weight: normal; vertical-align:top;}
.c.source-c .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.c.source-c .li2 {font-weight: bold; vertical-align:top;}
.c.source-c .kw1 {color: #0000dd;}
.c.source-c .kw2 {color: #0000ff;}
.c.source-c .kw3 {color: #0000dd;}
.c.source-c .kw4 {color: #0000ff;}
.c.source-c .co1 {color: #909090;}
.c.source-c .co2 {color: #339900;}
.c.source-c .coMULTI {color: #ff0000; font-style: italic;}
.c.source-c .es0 {color: #008000; font-weight: bold;}
.c.source-c .es1 {color: #008000; font-weight: bold;}
.c.source-c .es2 {color: #008000; font-weight: bold;}
.c.source-c .es3 {color: #008000; font-weight: bold;}
.c.source-c .es4 {color: #008000; font-weight: bold;}
.c.source-c .es5 {color: #008000; font-weight: bold;}
.c.source-c .br0 {color: #008000;}
.c.source-c .sy0 {color: #008000;}
.c.source-c .sy1 {color: #000080;}
.c.source-c .sy2 {color: #000040;}
.c.source-c .sy3 {color: #000040;}
.c.source-c .sy4 {color: #008080;}
.c.source-c .st0 {color: #008000;}
.c.source-c .nu0 {color: #000080;}
.c.source-c .nu6 {color:#000080;}
.c.source-c .nu8 {color:#000080;}
.c.source-c .nu12 {color:#000080;}
.c.source-c .nu16 {color:#000080;}
.c.source-c .nu17 {color:#000080;}
.c.source-c .nu18 {color:#000080;}
.c.source-c .nu19 {color:#000080;}
.c.source-c .ln-xtra, .c.source-c li.ln-xtra, .c.source-c div.ln-xtra {background-color: #ffc;}
.c.source-c span.xtra { display:block; }

/*]]>*/
</style><style type="text/css">/*<![CDATA[*/
.source-text {line-height: normal;}
.source-text li, .source-text pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for text
 * CSS class: source-text, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.text.source-text .de1, .text.source-text .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.text.source-text  {font-family:monospace;}
.text.source-text .imp {font-weight: bold; color: red;}
.text.source-text li, .text.source-text .li1 {font-weight: normal; vertical-align:top;}
.text.source-text .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.text.source-text .li2 {font-weight: bold; vertical-align:top;}
.text.source-text .ln-xtra, .text.source-text li.ln-xtra, .text.source-text div.ln-xtra {background-color: #ffc;}
.text.source-text span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-c_language_storage_duration skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">存储类指定符</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../c.html" title="c">c</a>‎ | <a href="../language.html" title="c/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../c.html" title="c"> C</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="c/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="c/header">头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../types.html" title="c/types">类型支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../program.html" title="c/program">程序工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../variadic.html" title="c/variadic">变参数函数支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../memory.html" title="c/memory">动态内存管理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="c/error">错误处理</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../chrono.html" title="c/chrono">日期和时间工具</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="c/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="c/algorithm">算法</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="c/numeric">数值</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="c/io">输入/输出支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="c/locale">本地化支持</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="c/thread">线程支持</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="c/atomic">原子操作</a> <span class="t-mark-rev t-since-c11">(C11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="c/language">C 语言</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv-h2"><td colspan="5"> <a href="basic_concepts.html" title="c/language/basic concepts">基本概念</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../keyword.html" title="c/keyword">关键词</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../preprocessor.html" title="c/preprocessor">预处理器</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="statements.html" title="c/language/statements">语句</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="expressions.html" title="c/language/expressions">表达式</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="initialization.html" title="c/language/initialization">初始化</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="declarations.html" title="c/language/declarations">声明</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="functions.html" title="c/language/functions">函数</a></td></tr>
<tr class="t-nv"><td colspan="5"> 杂项</td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="history.html" title="c/language/history">C 的历史</a></td></tr>
<tr class="t-nv-h2"><td colspan="5"> <a href="../experimental.html" title="c/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="declarations.html" title="c/language/declarations">声明</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="pointer.html" title="c/language/pointer">指针</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="array.html" title="c/language/array">数组</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="enum.html" title="c/language/enum"><span class="t-lines"><span>enum</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="struct.html" title="c/language/struct"><span class="t-lines"><span>struct</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="union.html" title="c/language/union"><span class="t-lines"><span>union</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="bit_field.html" title="c/language/bit field">位域</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="atomic.html" title="c/language/atomic">原子类型</a> <span class="t-mark-rev t-since-c11">(C11)</span></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="const.html" title="c/language/const"><span class="t-lines"><span>const</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="volatile.html" title="c/language/volatile"><span class="t-lines"><span>volatile</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="restrict.html" title="c/language/restrict"><span class="t-lines"><span>restrict</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c99">(C99)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Alignas.html" title="c/language/ Alignas"><span class="t-lines"><span>_Alignas</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink">存储期与链接</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="extern.html" title="c/language/extern">外部及试探性定义</a> </td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="typedef.html" title="c/language/typedef"><span class="t-lines"><span>typedef</span></span></a></div></div></td></tr>
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="_Static_assert.html" title="c/language/ Static assert"><span class="t-lines"><span>_Static_assert</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div></div></td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p>指定对象和函数的<i>存储期（ storage duration ）</i>和<i>链接（ linkage ）</i>：
</p>
<dl><dd><ul><li><code><b>auto</b></code> - 自动存储期与无链接
</li><li><code><b>register</b></code> - 自动存储期与无链接；不能取这种对象的地址
</li><li><code><b>static</b></code> - 静态存储期与内部链接（除非在块作用域）
</li><li><code><b>extern</b></code> - 静态存储期与外部链接（除非已声明带内部链接）
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<dl><dd><ul><li><code><b>_Thread_local</b></code> - 线程存储期
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<h3><span class="mw-headline" id=".E8.A7.A3.E9.87.8A">解释</span></h3>
<p>存储类指定符出现于<a href="declarations.html" title="c/language/declarations">声明</a>中。至多可使用一个指定符<span class="t-rev-inl t-since-c11"><span>，除了能将 <code>_Thread_local</code> 与 <code>static</code> 或 <code>extern</code> 组合以调整链接</span> <span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span>。存储类指定符确定其所声明的名称的二个独立属性：<i>存储期</i>与<i>链接</i>。
</p>
<div class="t-li1"><span class="t-li">1)</span> <code>auto</code> 指定符只对声明于块作用域的对象（除了函数参数列表）允许。它指示自动存储期与无链接，也是这种声明的默认属性。</div>
<div class="t-li1"><span class="t-li">2)</span> <code>register</code> 指定符只对声明于块作用域的对象允许，包括函数参数列表。它指示自动存储期与无链接（即这种声明的默认属性），但另外提示优化器，若可能则将此对象的值存储于 CPU 寄存器中。无论此优化是否发生，声明为 <code>register</code> 的对象不能用作<a href="operator_member_access.html" title="c/language/operator member access">取址运算符</a>的参数<span class="t-rev-inl t-since-c11"><span>，不能用 <a href="_Alignas.html" title="c/language/ Alignas">_Alignas</a></span> <span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span>，而且 <code>register</code> 数组不能转换为指针。</div>
<div class="t-li1"><span class="t-li">3)</span> <code>static</code> 指定符指定静态存储期<span class="t-rev-inl t-since-c11"><span>（除非与 <code>_Thread_local</code> 组合）</span> <span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span>和内部链接（除非用于块作用域）。它能用于在文件作用域的函数，以及文件和块作用域的对象，但不能用于函数参数列表。</div>
<div class="t-li1"><span class="t-li">4)</span> <code>extern</code> 指定静态存储期<span class="t-rev-inl t-since-c11"><span>（除非与 <code>_Thread_local</code> 组合）</span> <span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span>和外部链接。它能用于文件和块作用域中的函数和对象声明（除了函数参数列表）。若 <code>extern</code> 出现在已经声明带内部链接的标识符的再声明上，则链接仍为内部。否则（若前一声明为外部、无链接或不在作用域内）链接为外部。</div>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<div class="t-li1"><span class="t-li">5)</span> <code>_Thread_local</code> 指示<i>线程存储期</i>。它不能用于函数声明。若将它用在对象声明上，则它必须在同一对象的每次声明上都存在。若将它用在块作用域声明上，则必须与 <code>static</code> 或 <code>extern</code> 之一组合以决定链接。</div>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<p>若不提供存储类指定符，则默认为：
</p>
<dl><dd> 对所有函数为 <code>extern</code>
</dd><dd> 对在文件作用域的对象为 <code>extern</code>
</dd><dd> 对在块作用域的对象为 <code>auto</code>
</dd></dl>
<p>对于任何用存储类指定符声明的结构体或联合体，存储期（但非链接）递归地应用到其成员。
</p><p>在块作用域的函数声明能使用 <code>extern</code> 或完全不使用存储类指定符。在文件作用域的函数声明能使用 <code>extern</code> 或 <code>static</code> 。
</p><p>函数参数不能使用异于 <code>register</code> 的存储类指定符。注意 <code>static</code> 在数组类型的函数参数中有特殊含义。
</p>
<h3><span class="mw-headline" id=".E5.AD.98.E5.82.A8.E6.9C.9F">存储期</span></h3>
<p>每个<a href="object.html" title="c/language/object">对象</a>都有称为<i>存储期</i>的属性，它限制对象的<a href="lifetime.html" title="c/language/lifetime">生存期</a>。 C 中有四种存储期：
</p>
<dl><dd><ul><li><i><b>自动</b></i>存储期。进入声明对象于其中的<a href="statements.html#.E5.A4.8D.E5.90.88.E8.AF.AD.E5.8F.A5" title="c/language/statements">块</a>时分配其存储，而在以任何方式（ <a href="goto.html" title="c/language/goto">goto</a> 、 <a href="return.html" title="c/language/return">return</a> 、抵达结尾）退出该块时解分配存储。<span class="t-rev-inl t-since-c99"><span>一个例外是 <a href="array.html#.E5.8F.98.E9.95.BF.E5.BA.A6.E6.95.B0.E7.BB.84" title="c/language/array">VLA</a> ；在执行声明时而非块入口分配其存储，并在声明离开作用域而非退出块时解分配存储。</span> <span><span class="t-mark-rev t-since-c99">(C99 起)</span></span></span>若递归地进入块，则对每个递归层进行新的分配。所有函数参数和非 <code>static</code> 块作用域对象，还有用于块作用域的<a href="compound_literal.html" title="c/language/compound literal">复合字面量</a>拥有此存储期。
</li></ul>
</dd></dl>
<dl><dd><ul><li><i><b>静态</b></i>存储期。存储期是整个程序的执行过程，只在 <a href="main_function.html" title="c/language/main function"><code>main</code> 函数</a>之前初始化一次存储于对象的值。所有声明为 <code>static</code> 对象和所有带内部或外部链接<span class="t-rev-inl t-since-c11"><span>且不声明为 <code>_Thread_local</code> </span> <span><span class="t-mark-rev t-since-c11">(C11 起)</span></span></span>的对象都拥有此存储期。
</li></ul>
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-since-c11"><td>
<dl><dd><ul><li><i><b>线程</b></i>存储期。存储期是创建对象的线程的整个执行过程，在启动线程时初始化存储于对象的值。每个线程拥有其自身的相异对象。若执行访问此对象的表达式的线程，不是执行其初始化的线程，则行为是实现定义的。所有声明为 <code>_Thread_local</code> 的对象拥有此存储期。
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-c11">(C11 起)</span></td></tr>
</table>
<dl><dd><ul><li><i><b>分配</b></i>存储期。按照请求，用<a href="../memory.html" title="c/memory">动态内存分配</a>函数分配和解分配存储。
</li></ul>
</dd></dl>
<h4><span class="mw-headline" id=".E9.93.BE.E6.8E.A5">链接</span></h4>
<p>链接指的是在其他作用域指代一个标识符（具名对象或函数）的能力。若在数个作用域中声明有同一标识符的对象或函数，但不能从所有这些作用域指代它们，则会创建数个对象的实例。辨识下列链接：
</p>
<dl><dd><ul><li><i><b>无链接</b></i>。只能从其所在的作用域指代该标识符。所有函数参数和所有非 <code>extern</code> 的块作用域对象（包含声明为 <code>static</code> 者）拥有此链接。
</li></ul>
</dd></dl>
<dl><dd><ul><li><i><b>内部链接</b></i>。能从当前翻译单元的所有作用域指代该标识符。所有 <code>static</code> 文件作用域标识符（函数和对象）都拥有此链接。
</li></ul>
</dd></dl>
<dl><dd><ul><li><i><b>外部链接</b></i>。能从整个程序的任何其他翻译单元指代该标识符。所有非 <code>static</code> 函数、所有 <code>extern</code> 对象（除非之前声明为 <code>static</code> ）和所有文件作用域的非 <code>static</code> 对象拥有此链接。
</li></ul>
</dd></dl>
<p>若同一标识符在同一翻译单元中一同带内部和外部链接出现，则行为未定义。这在使用<a href="extern.html" title="c/language/extern">试探性定义</a>时有可能。
</p>
<h4><span class="mw-headline" id=".E9.93.BE.E6.8E.A5.E4.B8.8E.E5.BA.93">链接与库</span></h4>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：这或许应该为 <a href="../language.html" title="c/language">c/language</a> 中<b>杂项</b>下的顶层条目？ </td></tr></table>
<p>带外部链接的声明常在头文件中可用，这使得所有 <a href="../preprocessor/include.html" title="c/preprocessor/include">#include</a> 该头文件的翻译单元都可以指代定义于别处的相同标识符。
</p><p>任何出现于头文件中的带内部链接的声明，在每个包含该文件的翻译单元中产生一个分离而相异的对象或函数。
</p><p>库接口：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// flib.h</span>
<span class="co2">#ifndef FLIB_H</span>
<span class="co2">#define FLIB_H</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span><span class="sy4">;</span>              <span class="co1">// 带外部链接的函数声明</span>
<span class="kw2">extern</span> <span class="kw4">int</span> state<span class="sy4">;</span>          <span class="co1">// 带外部链接的对象声明</span>
<span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> size <span class="sy1">=</span> <span class="nu0">5</span><span class="sy4">;</span> <span class="co1">// 带内部链接的只读对象定义</span>
<span class="kw2">enum</span> <span class="br0">{</span> MAX <span class="sy1">=</span> <span class="nu0">10</span> <span class="br0">}</span><span class="sy4">;</span>         <span class="co1">// 常量定义</span>
<span class="kw1">inline</span> <span class="kw4">int</span> sum <span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">int</span> b<span class="br0">)</span> <span class="br0">{</span> <span class="kw1">return</span> a<span class="sy2">+</span>b<span class="sy4">;</span> <span class="br0">}</span> <span class="co1">// inline 函数定义</span>
<span class="co2">#endif // FLIB_H</span></pre></div></div>
<p>库实现：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// flib.c</span>
<span class="co2">#include "flib.h"</span>
<span class="kw4">static</span> <span class="kw4">void</span> local_f<span class="br0">(</span><span class="kw4">int</span> s<span class="br0">)</span> <span class="br0">{</span><span class="br0">}</span>  <span class="co1">// 带内部链接的定义（只用于此文件）</span>
<span class="kw4">static</span> <span class="kw4">int</span> local_state<span class="sy4">;</span>        <span class="co1">// 带内部链接的定义（只用于此文件）</span>
 
<span class="kw4">int</span> state<span class="sy4">;</span>                     <span class="co1">// 带外部链接的定义（ main.c 使用）</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span> <span class="br0">{</span>local_f<span class="br0">(</span>state<span class="br0">)</span><span class="sy4">;</span><span class="br0">}</span> <span class="co1">// 带外部链接的定义（ main.c 使用）</span></pre></div></div>
<p>应用代码：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co1">// main.c </span>
<span class="co2">#include "flib.h"</span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="kw4">int</span> x<span class="br0">[</span>MAX<span class="br0">]</span> <span class="sy1">=</span> <span class="br0">{</span>size<span class="br0">}</span><span class="sy4">;</span> <span class="co1">// 使用常量和只读变量</span>
    state <span class="sy1">=</span> <span class="nu0">7</span><span class="sy4">;</span>           <span class="co1">// 修改 flib.c 中的 state</span>
    f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>                 <span class="co1">// 调用 flib.c 中的 f()</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.85.B3.E9.94.AE.E8.AF.8D">关键词</span></h3>
<p><a href="../keyword/auto.html" title="c/keyword/auto"><tt>auto</tt></a>,
<a href="../keyword/register.html" title="c/keyword/register"><tt>register</tt></a>,
<a href="../keyword/static.html" title="c/keyword/static"><tt>static</tt></a>,
<a href="../keyword/extern.html" title="c/keyword/extern"><tt>extern</tt></a>,
<a href="../keyword/_Thread_local.html" title="c/keyword/ Thread local"><tt>_Thread_local</tt></a>
</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E6.84.8F">注意</span></h3>
<p>一般通过定义于头文件 <code>threads.h</code> 的便利宏 <span class="t-lc"><a href="../thread/thread_local.html" title="c/thread/thread local">thread_local</a></span> 使用关键词 <code>_Thread_local</code> 。
</p><p>C 语言文法中， <a href="typedef.html" title="c/language/typedef">typedef</a> 指定符在形式上列作存储类指定符，但它被用于声明类型名，而不指定存储。
</p><p>在文件作用域的 <code>const</code> 且非 <code>extern</code> 的名称在 C 中拥有外部链接（同所有文件作用域的默认情况），但在 C++ 中拥有内部链接。
</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</span></h3>
<div class="t-example"><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">运行此代码</div></div>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="c source-c"><pre class="de1"><span class="co2">#include &lt;stdio.h&gt;</span>
<span class="co2">#include &lt;stdlib.h&gt;</span>
 
<span class="kw4">int</span> A<span class="sy4">;</span> <span class="co1">// 静态存储期</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="kw4">void</span><span class="br0">)</span>
<span class="br0">{</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"&amp;A = %p<span class="es1">\n</span>"</span>, <span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span><span class="br0">)</span><span class="sy3">&amp;</span>A<span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// 自动存储期</span>
    <span class="kw4">int</span> A <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>   <span class="co1">// 隐藏全局 A</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"&amp;A = %p<span class="es1">\n</span>"</span>, <span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span><span class="br0">)</span><span class="sy3">&amp;</span>A<span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// 分配存储期</span>
    <span class="kw4">int</span> <span class="sy2">*</span>ptr_1 <span class="sy1">=</span> <a href="../memory/malloc.html"><span class="kw403">malloc</span></a><span class="br0">(</span><span class="kw4">sizeof</span><span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>   <span class="co1">// 开始分配存储期</span>
    <a href="../io/fprintf.html"><span class="kw851">printf</span></a><span class="br0">(</span><span class="st0">"address of int in allocated memory = %p<span class="es1">\n</span>"</span>, <span class="br0">(</span><span class="kw4">void</span><span class="sy2">*</span><span class="br0">)</span>ptr_1<span class="br0">)</span><span class="sy4">;</span>
    <a href="http://zh.cppreference.com/w/c/memory/free"><span class="kw406">free</span></a><span class="br0">(</span>ptr_1<span class="br0">)</span><span class="sy4">;</span>                        <span class="co1">// 停止分配存储期</span>
<span class="br0">}</span></pre></div></div>
<p>可能的输出：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">&amp;A = 0x600ae4
&amp;A = 0x7ffefb064f5c
address of int in allocated memory = 0x1f28c30</pre></div></div> 
</div>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-11">
<ul><li> C11 standard (ISO/IEC 9899:2011): 
</li></ul>
<dl><dd><ul><li> 6.2.2 Linkages of identifiers (p: 36-37)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.4 Storage durations of objects (p: 38-39)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.7.1 Storage-class specifiers (p: 109-110)
</li></ul>
</dd></dl>
<div class="t-ref-std-c99">
<ul><li> C99 standard (ISO/IEC 9899:1999): 
</li></ul>
<dl><dd><ul><li> 6.2.2 Linkages of identifiers (p: 30-31)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.2.4 Storage durations of objects (p: 32)
</li></ul>
</dd></dl>
<dl><dd><ul><li> 6.7.1 Storage-class specifiers (p: 98-99)
</li></ul>
</dd></dl>
<div class="t-ref-std-c89">
<ul><li> C89/C90 standard (ISO/IEC 9899:1990): 
</li></ul>
<dl><dd><ul><li> 3.1.2.2 Linkages of identifiers 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.1.2.4 Storage durations of objects 
</li></ul>
</dd></dl>
<dl><dd><ul><li> 3.5.1 Storage-class specifiers 
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<table class="t-dsc-begin">

</table></div>
</div>

<!-- 
NewPP limit report
Preprocessor visited node count: 2650/1000000
Preprocessor generated node count: 9680/1000000
Post‐expand include size: 50371/2097152 bytes
Template argument size: 11186/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:1076-0!*!0!!zh!*!zh!* and timestamp 20200216161936 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=c/language/storage_duration&amp;oldid=59319">https://zh.cppreference.com/mwiki/index.php?title=c/language/storage_duration&amp;oldid=59319</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/c/language/storage_duration">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2019年1月21日 (星期一) 03:24。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过5,792次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.511 secs. -->
	</body>
<!-- Cached 20200216161936 -->
</html>